package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.BathroomGridAdapter;
import com.wnhz.work.yz.bean.BathroomGoodsBean;
import com.wnhz.work.yz.bean.EntiretyBathroom2Bean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---整体卫浴详情列表
 */
public class BathroomGridActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private ImageView iv_right;
    private GridView my_gridView;
    private BathroomGridAdapter mAdapter;

    private String titles;
    private List<BathroomGoodsBean.InfoBean> beanList = new ArrayList<>();
    private String cateId;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bathroom_grid);
        EventBus.getDefault().register(this);
        initData();
        upload();
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("cateId", cateId);
        XUtil.Post(Url.PACKAGE_GETPACKAGELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    Log.e("=====BathroomGrid=====", result);
                    if (res.equals("1")) {
                        Gson gson = new Gson();
                        BathroomGoodsBean bathroomGoodsBean = gson.fromJson(result, BathroomGoodsBean.class);
                        beanList = bathroomGoodsBean.getInfo();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void initData() {
        titles = getIntent().getStringExtra("title");
        cateId = getIntent().getStringExtra("cateId");
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private static final String TAG = "BathroomGridActivity";

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(titles);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        iv_right = (ImageView) findViewById(R.id.view_main_right_image);
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.ic_datumoshi);

        my_gridView = (GridView) findViewById(R.id.my_gridView);

        mAdapter = new BathroomGridAdapter(this, beanList, my_gridView);
        my_gridView.setAdapter(mAdapter);
        my_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BathroomGridActivity.this, BathroomDetailActivity.class);
                intent.putExtra("packageId", beanList.get(position).getGoodsid());
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            case R.id.btn_main_right:
                intent = new Intent(this, EntiretyBathroom2Activity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
